package com.shijie.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.PostConstruct;

import org.apache.commons.io.IOUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import com.shijie.domain.entity.Regions;
import com.shijie.utils.JsonUtil;

@Service
public class RegionsService {
	private List<Regions> provinceList = new ArrayList<>();
	private Map<String,List<Regions>> cityMap = new HashMap<>();
	private Map<String,List<Regions>> areaMap = new HashMap<>();
	@Value("classpath:static/regions/province.json")  
	Resource provinceFators;
	@Value("classpath:static/regions/city.json")  
	Resource cityFators;
	@Value("classpath:static/regions/area.json")  
	Resource areaFators;
	@PostConstruct  
   public void init() throws Exception{
		//省份
        String json = IOUtils.toString(provinceFators.getInputStream(),"UTF-8");  
        JSONObject jsonObj = new JSONObject(json); 
        Iterator iter = jsonObj.keys();
        while(iter.hasNext()){
        	String countryId = (String)iter.next();
        	JSONArray provinces = jsonObj.optJSONArray(countryId);
        	for(int i=0;i<provinces.length();i++){
        		provinceList.add(JsonUtil.fromJson(provinces.getString(i), Regions.class));
        	}
        }
        
        //市
        String jsonx = IOUtils.toString(cityFators.getInputStream(),"UTF-8");  
        JSONObject jsonObjx = new JSONObject(jsonx); 
        for(Regions p : provinceList){
        	JSONArray citys = jsonObjx.optJSONArray(p.getId());
        	if(citys==null){
        		continue;
        	}
        	List<Regions> cities = new ArrayList<>();
        	for(int i=0;i<citys.length();i++){
        		cities.add(JsonUtil.fromJson(citys.getString(i), Regions.class));
        	}
        	cityMap.put(p.getId(), cities);
        }
        
        //area
        String jsony = IOUtils.toString(areaFators.getInputStream(),"UTF-8");  
        JSONObject jsonObjy = new JSONObject(jsony); 
        for (Entry<String, List<Regions>> entry : cityMap.entrySet()) {  
            for(Regions p : entry.getValue()){
            	JSONArray areas = jsonObjy.optJSONArray(p.getId());
            	if(areas==null){
            		continue;
            	}
            	List<Regions> ares = new ArrayList<>();
            	for(int i=0;i<areas.length();i++){
            		ares.add(JsonUtil.fromJson(areas.getString(i), Regions.class));
            	}
            	areaMap.put(p.getId(), ares);
            } 
        }  
    
   }  
	
   public  List<Regions> getAllProvince(){
	   return provinceList;
   }
	
   public  List<Regions> getCitiesByProvinceId(String provinceId){
	   return cityMap.get(provinceId);
   }
	
   public  List<Regions> getAreasByCityId(String cityId){
	   return areaMap.get(cityId);
   }
	
	 
}
